<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="电话号码" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入接收者号码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类别" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择状态" clearable size="small">
          <el-option label="注册" value="1" />
          <el-option label="登陆" value="2" />
          <el-option label="修改密码" value="3" />
          <el-option label="修改绑定手机" value="4" />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option label="成功" value="1" />
          <el-option label="失败" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:sendlog:export']"
          >导出</el-button
        >
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="sendlogList" @selection-change="handleSelectionChange">
      <el-table-column label="类别" align="center" prop="type" :formatter="typeFormatter" width="60" />
      <el-table-column label="手机号" align="center" prop="mobile" width="110" />
      <el-table-column label="验证码" align="center" prop="authCode" width="80" />
      <el-table-column label="短信内容" align="left" prop="content" :show-overflow-tooltip="true" />
      <el-table-column label="消息ID" align="center" prop="msgid" />
      <el-table-column label="结果" align="left" prop="result" :show-overflow-tooltip="true" />
      <el-table-column label="状态" align="center" prop="status" :formatter="statusFormatter" width="60" />
      <el-table-column label="下发时间" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listSendlog, getSendlog, delSendlog, exportSendlog } from '@/api/monitor/sendlog'

export default {
  name: 'Sendlog',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 验证码下发日志表格数据
      sendlogList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: undefined,
        mobile: undefined,
        authCode: undefined,
        content: undefined,
        msgid: undefined,
        result: undefined,
        memo: undefined,
        status: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        mobile: [{ required: true, message: '接收者号码不能为空', trigger: 'blur' }],
      },
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询验证码下发日志列表 */
    getList() {
      this.loading = true
      listSendlog(this.queryParams).then((response) => {
        this.sendlogList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        type: undefined,
        mobile: undefined,
        authCode: undefined,
        content: undefined,
        msgid: undefined,
        result: undefined,
        memo: undefined,
        status: '0',
        createTime: undefined,
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除验证码下发日志编号为"' + ids + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(function () {
          return delSendlog(ids)
        })
        .then(() => {
          this.getList()
          this.msgSuccess('删除成功')
        })
        .catch(function () {})
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出所有验证码下发日志数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(function () {
          return exportSendlog(queryParams)
        })
        .then((response) => {
          this.download(response.msg)
        })
        .catch(function () {})
    },
    statusFormatter(row, column) {
      let currentStatus = row.status
      if (currentStatus == '1') {
        return '成功'
      } else {
        return '失败'
      }
    },
    typeFormatter(row, column) {
      let currentType = row.type
      if (currentType == '1') {
        return '注册'
      } else if (currentType == '2') {
        return '登陆'
      } else if (currentType == '3') {
        return '修改密码'
      } else if (currentType == '4') {
        return '修改手机号'
      }
    },
  },
}
</script>
